package sisi.products;

import java.util.List;

import javax.persistence.EntityManager;


/**
 * ProductController.java - Controller class for the <i>product's database table</i>. 
 * @author Pedro Gonzalez pedro@sisisoftware.it
 * @author Claudio Pelosi claudio@sisisoftware.it
 * @author SISI Software S.n.c. - www.sisisoftware.it
 * @version 1.0, 12/11/2013
 * 
 */

public class ProductsController {

	private EntityManager getEntityManager() {
		return new sisi.MyEmf().getEntityManager();
	}

	public Product[] getProduct(String cerca) {
		List<Product> products = getProductList(cerca);
		return products.toArray(new Product[0]);
	}

	@SuppressWarnings("unchecked")
	public List<Product> getProductList(String cerca) {

		String cWhere = "";
		if (cerca != null && !cerca.isEmpty()) {
			cWhere = " WHERE TRIM( BOTH FROM UPPER(c.productname) ) LIKE :cerca ";
		}

		EntityManager em = getEntityManager();
		try {
			javax.persistence.Query q = em
					.createQuery("select c from Product as c " + cWhere
							+ " order by c.productname");
			
			if (cerca != null && !cerca.isEmpty()) {
				q.setParameter("cerca", '%'+cerca.trim().toUpperCase()+'%' );
			}
			
			List<Product> ar1 = q.getResultList();
			return ar1;
		} finally {
			em.close();
		}
	}

	public Product getProductxId(int id) {

		EntityManager em = getEntityManager();
		try {
			Product productx = em.find(Product.class, id);
			return productx;
		} finally {
			em.close();
		}
	}

	@SuppressWarnings("finally")
	public Product addProduct(Product product) {
		EntityManager em = getEntityManager();
		try {
			em.getTransaction().begin();
			em.persist(product);
			em.getTransaction().commit();
			product = refreshProduct(product);
		} finally {
			em.close();
			return product;
		}
	}

	@SuppressWarnings("finally")
	public boolean updateProduct(Product product) {

		EntityManager em = getEntityManager();
		try {
			em.getTransaction().begin();
			Product productx = em.find(Product.class, product.getId());
			productx.setId(product.getId());
			productx.setProductname(product.getProductname());
			productx.setPartno(product.getPartno());
			productx.setBert(product.getBert());
			productx.setCategoryid(product.getCategoryid());
			productx.setDtup(product.getDtup());
			productx.setFuel(product.getFuel());
			productx.setNote(product.getNote());
			productx.setOil(product.getOil());
			productx.setOth(product.getOth());
			productx.setPurcprice(product.getPurcprice());
			productx.setSaleprice(product.getSaleprice());
			productx.setSpare(product.getSpare());
			productx.setSupplierid(product.getSupplierid());
			em.getTransaction().commit();
			em.flush();

		} finally {
			em.close();
			return false;
		}
	}

	@SuppressWarnings("finally")
	public Product refreshProduct(Product product) {

		Product productx = product;
		EntityManager em = getEntityManager();
		try {
			em.getTransaction().begin();
			productx = em.find(Product.class, product.getId());
			em.flush();
			em.refresh(productx);
		} finally {
			em.close();
			return productx;
		}
	}

	@SuppressWarnings("finally")
	public boolean removeProduct(Product product) {
		EntityManager em = getEntityManager();
		try {
			em.getTransaction().begin();
			Product productx = em.find(Product.class, product.getId());
			em.remove(productx);
			em.getTransaction().commit();
		} finally {
			em.close();
			return false;
		}
	}

}
